package com.gmall.product.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseAttrInfo;
import com.gmall.model.product.BaseAttrValue;
import com.gmall.product.mapper.BaseAttrInfoMapper;
import com.gmall.product.mapper.BaseAttrValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseAttrService {

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    /**
     *
     * @param catalog1Id
     * @param catalog2Id
     * @param catalog3Id
     * @return
     */
    public Result<List<BaseAttrInfo>> attrInfoList(Long catalog1Id,
                                                   Long catalog2Id,
                                                   Long catalog3Id) {
        /**
         * SELECT * FROM base_attr_info
         * WHERE (category_id = 2 and category_level =1 )
         * or  (category_id = 30 and category_level =2 )
         * or (category_id = 192 and category_level =3)
         * order by id
         */
        QueryWrapper<BaseAttrInfo> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BaseAttrInfo> queryWrapper1 = queryWrapper.lambda();
        if(catalog1Id!= null && catalog1Id !=0){
            queryWrapper1.or((t) -> {
                t.eq(BaseAttrInfo::getCategoryId, catalog1Id).eq(BaseAttrInfo::getCategoryLevel, 1);
            });
        }
        if(catalog2Id!= null && catalog2Id !=0){
            queryWrapper1.or(t1->{
                t1.eq(BaseAttrInfo::getCategoryId,catalog2Id).eq(BaseAttrInfo::getCategoryLevel,2);
            });
        }
        if(catalog3Id!= null && catalog3Id !=0) {
            queryWrapper1.or(t2 -> {
                t2.eq(BaseAttrInfo::getCategoryId, catalog3Id).eq(BaseAttrInfo::getCategoryLevel, 3);
            });
        }
        queryWrapper1.orderByAsc(BaseAttrInfo::getId);
        //属性列表
        List<BaseAttrInfo> baseAttrInfos = baseAttrInfoMapper.selectList(queryWrapper);

        baseAttrInfos.stream().forEach(baseAttrInfo -> {
            //每一个属性
            /**
             * SELECT * from base_attr_value where attr_id = 23
             * SELECT * from base_attr_value where attr_id = 24
             * SELECT * from base_attr_value where attr_id = 106
             * SELECT * from base_attr_value where attr_id = 107
             */
            QueryWrapper<BaseAttrValue> queryWrapper2  = new QueryWrapper<>();
            queryWrapper2.lambda().eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(queryWrapper2);
            baseAttrInfo.setAttrValueList(baseAttrValues);//属性值 list
        });
        return Result.ok(baseAttrInfos);
    }

    /**
     * inner join
     * @param catalog1Id
     * @param catalog2Id
     * @param catalog3Id
     * @return
     */
    public Result<List<BaseAttrInfo>> attrInfoList1(Long catalog1Id,
                                                   Long catalog2Id,
                                                   Long catalog3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.selectAttrInfoList1(catalog1Id, catalog2Id, catalog3Id);
        return Result.ok(baseAttrInfoList);
    }

    /**
     * 子查询
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    public Result<List<BaseAttrInfo>> attrInfoList2(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.selectAttrInfoList2(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    public Result<String> saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //
        if(baseAttrInfo!=null && baseAttrInfo.getId() != null){//带ID，是修改
            baseAttrInfoMapper.updateById(baseAttrInfo);
            //删除之前的属性值
            UpdateWrapper<BaseAttrValue> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            baseAttrValueMapper.delete(updateWrapper);

        }else{
            baseAttrInfoMapper.insert(baseAttrInfo);//
        }


        Long attrId = baseAttrInfo.getId();//属性ID
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        attrValueList.forEach(baseAttrValue ->{//N条
            baseAttrValue.setAttrId(attrId);
            baseAttrValueMapper.insert(baseAttrValue);
        });
        return Result.ok();
    }

    public Result<List<BaseAttrValue>> getAttrValueList(Long attrId) {

        //select * from base_attr_value where attr_id = 23
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseAttrValue::getAttrId,attrId);
        List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(queryWrapper);
        return Result.ok(baseAttrValues);
    }

    public Result<String> deleteAttrInfo(Long attrId) {

        // delete from base_attr_value where attr_id = #{attrId}
        UpdateWrapper<BaseAttrValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BaseAttrValue::getAttrId,attrId);
        baseAttrValueMapper.delete(updateWrapper);

        //delete from base_attr_info where id = 23
        baseAttrInfoMapper.deleteById(attrId);
        return Result.ok();

    }


}
